package com.leetcode.LC;

import java.util.HashMap;
import java.util.Map;

public class LC904 {
    public int totalFruit(int[] tree) {

        int max=1;
        Map<Integer, Integer> hashMap = new HashMap<>();
        int j=0;

        for (int i = 0; i < tree.length; i++) {
            hashMap.put(tree[i],i);

            if (hashMap.size()>2){
                int minIndex = tree.length - 1;
                for (Integer value : hashMap.values()) {
                    if (value<minIndex){
                        minIndex=value;
                    }
                }

                hashMap.remove(tree[minIndex]);
                j=minIndex+1;
            }
            max=Math.max(max,i-j+1);
        }

        return max;
    }

    public static void main(String[] args) {
        System.out.println(new LC904().totalFruit(new int[]{1, 2, 3, 2, 2}));
    }
}
